package elitech.vietnam.weatherfashion.service;

import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpResponseException;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

class PHPSoapService {

	private final String mNAMESPACE = "http://eli6.cafe24.com/fashion_services/";
	private final String mURL = "http://eli6.cafe24.com/fashion_services/services.php";
	
//	private final String mNAMESPACE = "http://192.168.1.14:801/";
//	private final String mURL = "http://192.168.1.14:801/services.php";

	private String mSoapAction = "";

	public String getRespone(String methodName, MyParameter parameter)
			throws IOException {

		mSoapAction = mNAMESPACE + methodName;

		SoapObject request = new SoapObject(mNAMESPACE, methodName);

		if (parameter != null) {
			for (int i = 0; i < parameter.size(); i++) {
				request.addProperty(parameter.getName(i), parameter.getValue(i));
			}
		}

		SoapSerializationEnvelope soapEnv = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		// soapEnv.dotNet = true;
		soapEnv.setOutputSoapObject(request);

		// soapEnv.enc = SoapEnvelope.;
		HttpTransportSE http;
		http = new HttpTransportSE(mURL);
		// http.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		http.debug = true;

		String result = null;
		// try {
		try {
			http.call(mSoapAction, soapEnv);

			SoapObject response = (SoapObject) soapEnv.bodyIn;

			if (response != null) {
				result = response.getProperty(0).toString();
				result = result.trim();
				if (result.equals("null"))
					result = "";
			} else {
				result = "";
			}
		} catch (HttpResponseException e) {
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		}
		return (result == null ? "" : result);
	}

	public String getResponeNonJson(String methodName, MyParameter parameter) {

		mSoapAction = mNAMESPACE + methodName;
		SoapObject request = new SoapObject(mNAMESPACE, methodName);

		if (parameter != null) {
			for (int i = 0; i < parameter.size(); i++) {
				request.addProperty(parameter.getName(i), parameter.getValue(i));
			}
		}

		SoapSerializationEnvelope soapEnv = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		soapEnv.setOutputSoapObject(request);

		HttpTransportSE http = new HttpTransportSE(mURL);
		http.debug = true;
		// little tweak here
		String results = null;
		try {
			http.call(mSoapAction, soapEnv);
			results = soapEnv.bodyIn.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return (results == null ? "" : formatStringResponseNonJsons(methodName,
				results));
	}

	// private static String RETURN_STRING_BEGIN = "Response{return=";
	// private static String RETURN_STRING_END = "; }";

	// private String formatStringResponse(String methodName, String respone) {
	// try {
	// respone = respone.substring(methodName.length()
	// + RETURN_STRING_BEGIN.length(), respone.length()
	// - RETURN_STRING_END.length());
	// } catch (Exception e) {
	// // TODO: handle exception
	// // Log.e("1111111111111111111111111","1");
	// }
	// return respone;
	// }

	private String formatStringResponseNonJsons(String methodName,
			String respone) {
		try {
			final String BEGIN = "Response{return=";
			final String END = "; }";

			respone = respone.substring(methodName.length() + BEGIN.length(),
					respone.length() - END.length());
		} catch (Exception e) {
			// TODO: handle exception
		}
		return respone;
	}
}
